 /**
 * Description: 系统权限业务实现类
 * Copyright:Copyright 2018 nfky.com. All rights reserved
 * @author:xk
 * @since:0.0.1
 * Create at:2018-01-10 下午 19:40:18
 *  
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2018-01-10   xk       0.0.1     Initial
 */
package com.metro.demo.sys.service.impl;

import com.metro.core.dao.IMapper;
import com.metro.core.excel.ExcelUtil;
import com.metro.core.service.BaseServiceSupport;
import com.metro.core.service.ServiceException;
import com.metro.core.vo.JsonResult;
import com.metro.core.vo.LabelValue;
import com.metro.core.vo.ResultCode;
import com.metro.core.vo.inf.IResult;
import com.metro.demo.common.beans.constant.CommonConstant;
import com.metro.demo.common.beans.constant.DicTypeEnum;
import com.metro.demo.common.beans.constant.PropertiesEnum;
import com.metro.demo.common.utils.DicUtil;
import com.metro.demo.sys.beans.vo.SysRoleMenu;
import com.metro.demo.sys.beans.vo.ZTreeNode;
import com.metro.demo.sys.mapper.ISysRoleMenuMapper;
import com.metro.demo.web.controller.imp.ImportResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.metro.demo.sys.mapper.ISysPermissionMapper;
import com.metro.demo.sys.service.inf.ISysPermissionService;
import com.metro.demo.sys.beans.vo.SysPermission;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

 /**
 * 系统权限业务实现类<br>
 *
 * @author xk
 * @since [0.0.1]
 * @version [0.0.1, 2018-01-10]
 */
@Service("sysPermissionService")
public class SysPermissionService extends BaseServiceSupport<SysPermission> implements ISysPermissionService{
    //系统权限Mapper
    @Autowired
    private ISysPermissionMapper sysPermissionMapper;
     @Autowired
     private ISysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public IMapper getMapper() {
        return sysPermissionMapper;
    }

     /**
      * 查询角色所拥有的权限列表
      */
     @Override
     public List<SysPermission> queryMenuByRoleIds(String roleIds) {
         String newRoleIds = "";
         String tempRoleId = "";
         for (int i = 0; i < roleIds.split(",").length; i++) {
             tempRoleId = roleIds.split(",")[i];
             newRoleIds += newRoleIds == "" ? "'" + tempRoleId + "'" : ",'" + tempRoleId + "'";
         }
         return sysPermissionMapper.queryMenuByRoleIds(newRoleIds);
     }

     @Override
     public List<ZTreeNode> queryPermissionTree() {
         List<ZTreeNode> list = sysPermissionMapper.queryPermissionTree();
         List<LabelValue> subSysList = (List<LabelValue>) DicUtil.getLabelValueList(DicTypeEnum.SUB_SYSTEM);
         for (int i = 0; i < subSysList.size(); i++ ) {
             LabelValue labelValue = subSysList.get(i);
             list.add(i, new ZTreeNode(labelValue.getValue(), "00", labelValue.getLabel(), true, "true"));
         }
         return list;
     }

     @Override
     public IResult deletePermissionAndSubById(Long id) {
         try {
             sysPermissionMapper.deletePermissionAndSubById(id);
             SysRoleMenu entity = new SysRoleMenu();
             entity.setMenuId(id);
             sysRoleMenuMapper.deleteByMenuId(entity);
             return ResultCode.SUCCESS;
         } catch (Exception e) {
             log.error("删除系统权限失败", e);
             throw new ServiceException(ResultCode.ERROR);
         }
     }

    @Override
    public IResult save(SysPermission entity) {
        try {
            sysPermissionMapper.save(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("新增系统权限失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult update(SysPermission entity) {
        try {
            sysPermissionMapper.update(entity);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("修改系统权限失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult delete(Long id) {
        try {
            sysPermissionMapper.delete(id);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

    @Override
    public IResult deleteByIds(String ids) {
        try {
            sysPermissionMapper.deleteByIds(ids);
            return ResultCode.SUCCESS;
        } catch (Exception e) {
            log.error("删除失败", e);
            throw new ServiceException(ResultCode.ERROR);
        }
    }

     @Override
     public JsonResult batchImport(String filePath) {
         JsonResult jr = new JsonResult();
         ImportResult result = new ImportResult();

         String rootDir = PropertiesEnum.getPropertyValue(PropertiesEnum.FILE_UPLOAD_ROOT);
         String realPath = rootDir + "/" + filePath;

         Map data = new HashMap();
         List<SysPermission> permissions = new ArrayList<>();
         data.put("permissions", permissions);
         try (InputStream excel = new FileInputStream(realPath)) {
             ExcelUtil.readByTemplate("/template/system/permission.xml", data, excel);
             sysPermissionMapper.deleteAll();
             for (SysPermission permission : permissions){
                 permission.setDataStatus(CommonConstant.YES);
                 this.save(permission);
             }
             result.setTotalNum(permissions.size());
             jr.setResult(ResultCode.SUCCESS);
             jr.setData(result);
         }catch (Exception ex){
             log.error("读取文件失败", ex);
             jr.setResult(ResultCode.ERROR);
         }
         return jr;
     }
}